<?php 
class product extends Controller {

var $data_product	 = '';
var $image_product	 = '';
var	$nid_products	 = '';

var $row_per_page	 = 30;

var $event			 = '';
var $data_view		 = '';

var	$title					= '';
var	$tags					= '';
var	$description			= ''; 

function product() 
	{
		parent::Controller();
		session_start();
		$this->load->database();
		$this->load->helper('ap_function');
		$this->load->helper('ap_object');
		$this->load->helper('ap_view');
		$this->load->helper('ap_module');
		$this->load->helper('ap_html');
		$this->load->model('product_model');
		$this->load->model('home_model');	
		$this->load->language('ap', 'eng');
	}

function brand($nid_material,$nid_cat,$nid_brand, $currentpage = 1)
{
	$current_page		= $currentpage; 
	$total_row 			= $this->product_model->get_count_list_product_by_brandid($nid_material,$nid_cat,$nid_brand);
	$total_page			= Fget_total_page($this->row_per_page,$total_row);

	$data_list = $this->product_model->get_list_product_by_brandid($nid_material,$nid_cat,$nid_brand,$total_row,$current_page,$this->row_per_page);
	
	$data['get_page']				= Fgen_pagination2($currentpage,$total_page, base_url(),'product/brand/'.$nid_material.'/'.$nid_cat.'/'.$nid_brand.'/');
	$data['menu_sec']				= $nid_material;
	$data['menu_cat']				= $nid_cat;
	$data['menu_brand']				= $nid_brand;
	$data['data_list'] 				= $data_list;
	
	$brand = get_brand_product_byid($nid_brand);
	$this->title	= $brand['cbrand_products'];
	
	$data['title']					= $this->title;
	$data['tags']					= $this->tags;
	$data['description']			= $this->description;
	$data['menu_top']				= 'product';
	$data['g_ishome']				= 0;
	$_SESSION['back_link']			= base_url().'product/brand/'.$nid_material.'/'.$nid_cat.'/'.$nid_brand.'/'.$currentpage;
	$this->load->view('list_brand_product',$data);
}
	
function list_product($nid_material,$nid_cat,$currentpage = 1)
{
	$current_page		= $currentpage; 
	$total_row 			= $this->product_model->get_count_list_product_by_catid($nid_material,$nid_cat);
	$total_page			= Fget_total_page($this->row_per_page,$total_row);

	$data_list = $this->product_model->get_list_product_by_catid($nid_material,$nid_cat,$total_row,$current_page,$this->row_per_page);
	
	$data['get_page']				= Fgen_pagination2($currentpage,$total_page, base_url(),'product/list_product/'.$nid_material.'/'.$nid_cat.'/');
	$data['menu_sec']				= $nid_material;
	$data['menu_cat']				= $nid_cat;
	$data['data_list'] 				= $data_list;
	$cat = get_cat_product_byid($nid_cat);
	$data['title']					= $cat['ccat_products'];
	$data['tags']					= $this->tags;
	$data['description']			= $this->description;
	$data['menu_top']				= 'product';
	$data['g_ishome']				= 0;
	$_SESSION['back_link']			= base_url().'product/list_product/'.$nid_material.'/'.$nid_cat.'/'.$currentpage;
	$this->load->view('list_product',$data);
}

function section($nid_material,$currentpage = 1)
{
	$current_page		= $currentpage; 
	$total_row 			= $this->product_model->get_count_list_product_by_matid($nid_material);
	$total_page			= Fget_total_page($this->row_per_page,$total_row);
	
	$data_list_by_matid = $this->product_model->get_list_product_by_matid($nid_material,$total_row,$current_page,$this->row_per_page);
	$data_list_special 	= $this->product_model->get_special_product_by_matid($nid_material);
	
	$data['get_page']				= Fgen_pagination2($currentpage,$total_page, base_url(),'product/section/'.$nid_material.'/');
	$data['menu_sec']				= $nid_material;
	$data['menu_cat']				= '';
	$data['data_list_by_matid'] 	= $data_list_by_matid;
	$data['data_list_special'] 		= $data_list_special;
	
	$material = Obj_get_material_product_byid($nid_material) ;
	foreach($material as $sec):
	$this->title	= $sec['cmaterial_products'];
	endforeach; 
	
	$data['title']					= $this->title;
	$data['tags']					= $this->tags;
	$data['description']			= $this->description;
	$data['menu_top']				= 'product';
	$data['g_ishome']				= 0;
	$_SESSION['back_link']			= base_url().'product/section/'.$nid_material.'/'.$current_page;
	$this->load->view('dssp_theo_chat_lieu',$data);
}

function sales($currentpage = 1)
{
	$current_page		= $currentpage; 
	$total_row 			= $this->product_model->get_count_list_product_sale();
	$total_page			= Fget_total_page($this->row_per_page,$total_row);
	
	$data_list_sale = $this->product_model->get_list_product_sale($total_row,$current_page,$this->row_per_page);
	
	$data['get_page']				= Fgen_pagination2($currentpage,$total_page, base_url(),'product/sales/');
	$data['menu_sec']				= '';
	$data['menu_cat']				= '';
	$data['data_list_sale'] 		= $data_list_sale;
	$data['title']					= $this->title;
	$data['tags']					= $this->tags;
	$data['description']			= $this->description;
	$data['menu_top']				= 'product';
	$data['g_ishome']				= 0;
	$_SESSION['back_link']			= base_url().'product/sales/'.$current_page;
	$this->load->view('product_sales',$data);
}

// Dinh nghia ham rut gon khi set language cho cac label.
// 
private function m_language_key($str_key)
	{
		return $this->lang->line('lbl.product.'.$str_key);
	}


function detail($nid)
	{
		$this->event	= 'detail';
		$this->nid_products = $nid;
		$_SESSION[session_prefix().'back_link']			= base_url().'product/detail/'.$nid;
		$this->do_process();
	}

function view_all_photo($nid)
	{
		$this->event	= 'view_all_photo';
		$this->nid_products = $nid;
		$data['title']				= $this->title;
		$data['tags']				= $this->tags;
		$data['description']		= $this->description;
		$data['menu_top']			= 'product';
		$_SESSION[session_prefix().'back_link']			= base_url().'product/view_all_photo/'.$nid;
		$this->do_process();
	}

function add_cart($nid)
{
	if(isset($_SESSION['cart'][$nid])){
		$qty = $_SESSION['cart'][$nid] + 1;
	}
	else
	{
		$qty=1;
	}

	$_SESSION['cart'][$nid]=$qty;
	
	if(isset($_SESSION['back_link']))
		 $back_url	= $_SESSION['back_link'];
	 else
		 $back_url	= base_url();
	redirect($back_url);
}
	
function index()
	{
		$_SESSION[session_prefix().'back_link']			= base_url().'index.php/product';
		$this->do_process();
	}
	
function do_process() 
	{
		$this->get_data(); 		
		$this->caculate_data(); 		
		$this->do_business(); 		
		$this->destroy_data();
	}
	
private function get_data()
	{  
		
	}
	
private function caculate_data()
	{
		if($this->event == 'detail' || $this->event == 'view_all_photo')
		{
			$this->data_product 		= $this->product_model->get_detail_byid($this->nid_products);
		}
		//else
			//$this->data_view 		= $this->product_model->get_listview();
	}
	
private function do_business()
	{	
		$data['menu_sec']				= '';
		$data['menu_cat']				= '';
		$data['g_ishome']				= 0;
		if($this->event == 'detail')
		{
			$data['obj_data'] 			= $this->data_product;
			$data['title']				= $this->data_product['cproducts'];
			$data['tags']				= $this->data_product['cdescription'];
			$data['description']		= $this->data_product['cdescription'];
			$data['menu_top']			= 'product';
			$this->load->view('detail',$data);	
		}
		elseif($this->event == 'view_all_photo')
		{
			$data['obj_data'] 			= $this->data_product;
			$data['title']				= $this->title;
			$data['tags']				= $this->tags;
			$data['description']		= $this->description;
			$data['menu_top']			= 'product';
			$this->load->view('view_all_photo',$data);	
		}
		else
		{
			$data['title']				= $this->title;
			$data['tags']				= $this->tags;
			$data['description']		= $this->description;
			$data['menu_top']			= 'product';
			$data['data_view'] 			= $this->data_view;
			$this->load->view('product',$data);	
		}
	}
	
private function destroy_data()
	{
				
	}
	

}